/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testws;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.*;
/**
 *
 * @author Tmblue
 */
public class webservice {

    public String getResponse(String URLstring)
    {
        String str = "";
        try {
        // Create a URL for the desired page
        URL url = new URL(URLstring);

        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String st = "";
        while ((st = in.readLine()) != null) {
            // str is one line of text; readLine() strips the newline character(s)
            str += "\n" + st;
        }
        in.close();

        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return str;
    }

    public String sendPOSTRequest(String URLstring)
    {
        String str = "";
        try {
            // Construct data
            String data = URLEncoder.encode("data[User][username]", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
            data += "&" + URLEncoder.encode("data[User][password]", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

            // Send data
            URL url = new URL(URLstring);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();

            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                str += line;
            }
            wr.close();
            rd.close();
        } catch (Exception e) {
        }

        return str;
    }

    public void sendCookie(String urlString)
    {
        try {
        // Create a URLConnection object for a URL
        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        // Set the cookie value to send
        conn.setRequestProperty("Cookie", "User.username=tmblue; User.password=maleand");

        // Send the request to the server
        conn.connect();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }

    }

    public void access(){
        // Install the custom authenticator
        Authenticator.setDefault(new MyAuthenticator());

        // Access the page
        try {
            // Create a URL for the desired page
            URL url = new URL("http://localhost/ws/test");

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            while ((str = in.readLine()) != null) {
                // str is one line of text; readLine() strips the newline character(s)
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
    }

     public class MyAuthenticator extends Authenticator {
        // This method is called when a password-protected URL is accessed
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            // Get information about the request
            String promptString = getRequestingPrompt();
            String hostname = getRequestingHost();
            InetAddress ipaddr = getRequestingSite();
            int port = getRequestingPort();

            // Get the username from the user...
            String username = "toan";

            // Get the password from the user...
            String password = "nguyen";

            // Return the information
            return new PasswordAuthentication(username, password.toCharArray());
        }
    }

}
